package com.jcrj.service;

import com.jcrj.dao.TaskDao;
import com.jcrj.entity.OMyOpinion;
import com.jcrj.entity.OTask;
import com.jcrj.entity.OTaskPeople;
import com.jcrj.entity.OTaskReport;
import com.jcrj.entity.OUser;
import com.jcrj.util.DateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TaskService {
	private TaskDao taskDao;

	public List<OTask> getQueryTaskCxList(int page, int userId, String title, String startTime, String endTime, String notifyUserId) {
		String hql = "from OTask t where t.userId=" + userId;
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and t.title like '%" + title.trim() + "%'";
		}
		if ((startTime != null) && (!"".equals(startTime)) && (endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and t.startTime>= to_date('" + startTime + "','yyyy-mm-dd') and t.startTime<=to_date('" + endTime
					+ "','yyyy-mm-dd') or t.endTime>=to_date('" + startTime + "','yyyy-mm-dd') and t.endTime<=to_date('" + endTime + "','yyyy-mm-dd') ";
		}
		if ((notifyUserId != null) && (!"".equals(notifyUserId))) {
			hql = hql + " and t.id in(select o.taskId from OTaskPeople o where o.userId=" + notifyUserId + ")";
		}

		hql = hql + " order by t.creationTime desc";
		return this.taskDao.findByPage(hql, page);
	}

	public Long countTaskNotify(int userId) {
		String hql = "select count(t.id) from OTask t where  t.id in(select m.taskId from OTaskPeople m where m.userId=" + userId + ")";
		return this.taskDao.countTotalByHql(hql);
	}

	public List<OTask> getTaskNotifyList(int page, int userId) {
		String hql = "from OTask t where  t.id in(select m.taskId from OTaskPeople m where m.userId=" + userId + ") order by t.creationTime desc";
		return this.taskDao.findByPage(hql, page);
	}

	public void updateTaskSet(OTaskReport taskReport) {
		this.taskDao.updateReport(taskReport);
	}

	public OTaskReport getTaskReport(int id) {
		return (OTaskReport) this.taskDao.getObject(new OTaskReport(), id);
	}

	public void deleteTaskSet(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from OTaskReport where id in(" + id + ")";
			this.taskDao.executeHql(hql);
		}
	}

	public void saveTastSet(OTaskReport taskReport, String selectUserId) {
		if ((selectUserId != null) && (!"".equals(selectUserId))) {
			String[] ids = selectUserId.split(",");
			String[] names = taskReport.getSelectName().split(",");
			for (int i = 0; i < ids.length; i++) {
				OTaskReport report = new OTaskReport();
				report.setStartDate(taskReport.getStartDate());
				report.setEndDate(taskReport.getEndDate());
				report.setSelectUserId(Integer.valueOf(Integer.parseInt(ids[i])));
				report.setSelectName(names[i]);
				report.setUserId(taskReport.getUserId());
				this.taskDao.saveReport(report);
			}
		}
	}

	public Long countTaskSet(int id) {
		String hql = "select count(id) from OTaskReport where userId=" + id;
		return this.taskDao.countTotalByHql(hql);
	}

	public List<OTaskReport> getTaskSetList(int id, int page) {
		String hql = "from OTaskReport where userId=" + id;
		return this.taskDao.findByPage(hql, page);
	}

	public List<OTask> getTask(String time, int id) {
		String hql = "from OTask o where o.userId=" + id + " and o.startTime <=to_date('" + time + "','yyyy-mm-dd') and o.endTime>=to_date('" + time
				+ "','yyyy-mm-dd')";
		return this.taskDao.findByHql(hql);
	}

	public List<OTask> getTaskByMonth(Date date, int id) {
		String startTime = DateUtil.dateFirstDay(date);
		String lastTime = DateUtil.dateLastDay(date);
		String hql = "from OTask o where o.userId=" + id + " and  to_date('" + startTime + "','yyyy-mm-dd')<= o.startTime and  to_date('" + lastTime
				+ "','yyyy-mm-dd')>= o.startTime or o.userId=" + id + " and to_date('" + startTime + "','yyyy-mm-dd')<= o.endTime and  to_date('" + lastTime
				+ "','yyyy-mm-dd')>= o.endTime";
		return this.taskDao.findByHql(hql);
	}

	public Long countTodoTask(String userId, String time) {
		String hql = "select count(id) from OTask t where   userId in(" + userId + ")  and  to_date('" + time + "','yyyy-mm-dd')<= t.endTime and  to_date('"
				+ time + "','yyyy-mm-dd')>= t.startTime or (t.id in(select m.taskId from OTaskPeople m where m.userId in(" + userId + ")) and  to_date('"
				+ time + "','yyyy-mm-dd')<= t.endTime and  to_date('" + time + "','yyyy-mm-dd')>= t.startTime)";
		return this.taskDao.countTotalByHql(hql);
	}

	public List<OTask> getTodoTaskList(String userId, String time, int page) {
		String hql = "from OTask t where userId in(" + userId + ")  and  to_date('" + time + "','yyyy-mm-dd')<= t.endTime and  to_date('" + time
				+ "','yyyy-mm-dd')>=t.startTime  or (t.id in(select m.taskId from OTaskPeople m where m.userId in(" + userId + "))  and  to_date('" + time
				+ "','yyyy-mm-dd')<= t.startTime and  to_date('" + time + "','yyyy-mm-dd')>= t.startTime) order by t.creationTime desc";
		return this.taskDao.findByPage(hql, page);
	}

	public void updateTaskStatus(String status, int id) {
		String hql = "update OTask set status=" + status + " where id=" + id;
		this.taskDao.executeHql(hql);
	}

	public List<OMyOpinion> getopinTask(int taskId) {
		String hql = "from OMyOpinion where taskId=" + taskId + "order by addTime desc";
		return this.taskDao.findByHql(hql);
	}

	public void saveOpinionTask(OMyOpinion opinion) {
		this.taskDao.saveReport(opinion);
	}

	public void updateTask(OTask task) {
		if ((task.getNotifyUserId() != null) && (!"".equals(task.getNotifyUserId()))) {
			String hql = "delete from OTaskPeople where userId in (" + task.getNotifyUserId() + ")";
			this.taskDao.executeHql(hql);
			String[] id = task.getNotifyUserId().split(",");
			for (int i = 0; i < id.length; i++) {
				OTaskPeople taskPeople = new OTaskPeople();
				taskPeople.setTaskId(task.getId());
				taskPeople.setUserId(Integer.valueOf(Integer.parseInt(id[i])));
				this.taskDao.saveReport(taskPeople);
			}
		} else {
			String hql = "delete from OTaskPeople where task_id in (" + task.getId() + ")";
			this.taskDao.executeHql(hql);
		}
		this.taskDao.updateReport(task);
	}

	public void deleteTask(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "delete from OTask where id in(" + id + ")";
			String hql_2 = "delete from OTaskPeople where taskId in(" + id + ")";
			this.taskDao.executeHql(hql);
			this.taskDao.executeHql(hql_2);
		}
	}

	public OTask getTask(int id) {
		return (OTask) this.taskDao.getObject(new OTask(), id);
	}

	public Long countTask(int id) {
		String hql = "select count(id) from OTask where userId=" + id;
		return this.taskDao.countTotalByHql(hql);
	}

	public List<OTask> getTask(int id, int page) {
		String hql = "from OTask where userId=" + id + " order by creationTime desc";
		return this.taskDao.findByHql(hql);
	}

	public List<OTaskReport> getTsReportList(int id) {
		String hql = "from OTaskReport userId='" + id + "'";
		return this.taskDao.findByHql(hql);
	}

	public void saveTase(OTask oTask, String userid) {
		this.taskDao.saveReport(oTask);
		if ((userid != null) && (!"".equals(userid))) {
			String[] id = userid.split(",");
			for (int i = 0; i < id.length; i++) {
				OTaskPeople taskPeople = new OTaskPeople();
				taskPeople.setTaskId(oTask.getId());
				taskPeople.setUserId(Integer.valueOf(Integer.parseInt(id[i])));
				this.taskDao.saveReport(taskPeople);
			}
		}
	}

	public OUser getUserNameNew(int id) {
		return (OUser) this.taskDao.getObject(new OUser(), id);
	}

	public List<OUser> getUserName(String[] id, String ids) {
		if (id != null) {
			ids = "";
			for (int i = 0; i < id.length; i++) {
				if (ids == "")
					ids = ids + id[i];
				else {
					ids = ids + "," + id[i];
				}
			}
		}
		String hql = "from OUser where id in (" + ids + ")";
		return this.taskDao.findByHql(hql);
	}

	public List<Integer> getAgentUser(OUser user) {
		String nowDate = DateUtil.DateToString(new Date());
		String hql = " from OTaskReport t where selectUserId =" + user.getId() + " and startDate<=to_date('" + nowDate + "','yyyy-mm-dd') and to_date('"
				+ nowDate + "','yyyy-mm-dd')<=endDate";
		List<OTaskReport> list = this.taskDao.findByHql(hql);
		List userList = new ArrayList();
		if ((list != null) && (!list.isEmpty())) {
			for (OTaskReport report : list) {
				userList.add(report.getUserId());
			}
		}
		return userList;
	}

	public TaskDao getTaskDao() {
		return this.taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.service.TaskService JD-Core Version: 0.6.0
 */